package com.stepforward.me;

import java.util.ArrayList;



import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.util.SparseBooleanArray;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;


public class FavoriteRoute extends Activity {
    /** Called when the activity is first created. */
	MyFavoriteHelper dbHelper;
	private ListView wordsList;
	private TextView txtFav;
	private static final int REQUEST_CODE = 1234;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fav);
        wordsList = (ListView) findViewById(R.id.txtList);
        dbHelper=new MyFavoriteHelper(this);
  
        
        ArrayList<String> matches= dbHelper.getAllFavoritesName();
        if(matches!=null)
        {
        wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked,matches));
        wordsList.setChoiceMode(wordsList.CHOICE_MODE_SINGLE);
        }
        txtFav=(TextView)findViewById(R.id.txtFav);
        txtFav.setText(txtFav.getText()+String.valueOf(dbHelper.getFavCount()));
        wordsList.setItemChecked(2, true);
        wordsList.setOnItemLongClickListener(
        		new OnItemLongClickListener(){

					@Override
					public boolean onItemLongClick(AdapterView<?> parent,
							View arg1, int position, long arg3) {

						String name=(String) parent.getItemAtPosition(position);
						Intent addIntent=new Intent();
						addIntent.putExtra("departure", dbHelper.getDepByName(name));
						addIntent.putExtra("arrival",dbHelper.getArrByName(name));
						addIntent.putExtra("name",name);
						addIntent.setAction("GoFavRoute");
						startActivity(addIntent);

									
									
						return false;
					}});
        wordsList.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				
				Toast.makeText(FavoriteRoute.this, "Long Click will bring you to the Route",
						Toast.LENGTH_SHORT).show();
				
			}});
 
    }

@Override
protected void onResume(){
    super.onResume();
    wordsList = (ListView) findViewById(R.id.txtList);
   txtFav=(TextView)findViewById(R.id.txtFav);
    dbHelper=new MyFavoriteHelper(this);
    ArrayList<String> matches= dbHelper.getAllFavoritesName();
    if(matches!=null)
    {
    wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked,matches));
    }
    txtFav.setText("Number of favorites "+String.valueOf(dbHelper.getFavCount()));
    // code to update the date here
}

    public void deleteButtonClicked(View view)
    
    {

    		SparseBooleanArray selectedItems = wordsList.getCheckedItemPositions();
    		for (int i = 0; i < selectedItems.size(); i++) {
    		    if (selectedItems.get(i)) {
    		        String name = (String) wordsList.getAdapter().getItem(
    		            selectedItems.keyAt(i));
    		        dbHelper.DeleteFav(name);
    		        }
    		        
    		    }
            ArrayList<String> matches= dbHelper.getAllFavoritesName();
    		wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked,matches));
            txtFav=(TextView)findViewById(R.id.txtFav);
			txtFav.setText("Number of favorites "+String.valueOf(dbHelper.getFavCount()));
    		
    	}
    public void addButtonClicked(View view)
    {
		Intent addIntent=new Intent();
		addIntent.setAction("AddFavorite");
		addIntent.putExtra("flag", "false");
		startActivity(addIntent);
		
    }
  
}
 

